package cqu.java.lock;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class MyService {
	private Lock lock = new ReentrantLock();
	private Condition  condition = lock.newCondition();
	
	public void awaintA() {
		try {
			lock.lock();
			System.out.println("begin awaitA time = "+System.currentTimeMillis()+"==>"+Thread.currentThread().getName());
			//等待通知
			condition.await();
			System.out.println("end awaitA time = "+System.currentTimeMillis()+"==>"+Thread.currentThread().getName());
		} catch (Exception e) {
			e.printStackTrace();
		}finally {
			//System.out.println("AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA");
			lock.unlock();
		}
	}
	
	public void awaintB() {
		try {
			lock.lock();
			System.out.println("begin awaitB time = "+System.currentTimeMillis()+"==>"+Thread.currentThread().getName());
			condition.await();
			System.out.println("end awaitB time = "+System.currentTimeMillis()+"==>"+Thread.currentThread().getName());
		} catch (Exception e) {
			e.printStackTrace();
		}finally {
			//System.out.println("BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB");
			lock.unlock();
		}
	}
	
	public void waitC() {
		lock.lock();
		System.out.println("我也想要锁lock");
		lock.unlock();
	}
	
	public void signalAll() {
		try {
			lock.lock();
			System.out.println(" signalAll time = "+System.currentTimeMillis()+"==>"+Thread.currentThread().getName());
			condition.signalAll();
		}finally{
			lock.unlock();
		}
	}
}
